package com.example.farmeri.presenter.message;

import com.example.farmeri.data.DataParam;
import com.example.farmeri.data.RetrofitManager;
import com.example.farmeri.http.API;
import com.example.farmeri.presenter.base.BasePresenter;
import com.example.farmeri.tools.ResponseUtils;
import com.example.farmeri.ui.users.model.User;
import com.example.farmeri.ui.users.model.UserList;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.FormBody;
import okhttp3.RequestBody;

public class MessageSendPresenter extends BasePresenter<IMessageSendContract> {


    public MessageSendPresenter(IMessageSendContract view) {
        super(view);
    }


    public void getUsers(int page, int limit) {
        getAPI().getAllUser(page,limit)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObservable<ResponseUtils<UserList>>() {
                    @Override
                    public void onSuccess(ResponseUtils<UserList> t) {
                        getView().onUserInfoSuccess(t.getData());
                    }

                    @Override
                    public void onError(String msg) {
                        getView().onUserInfoError(msg);
                    }
                });
    }

    public void sendMessage(Map<String, String> map){
        getAPI().addMessage(createFormBody(map))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObservable<ResponseUtils<String >>() {
                    @Override
                    public void onSuccess(@NonNull ResponseUtils<String > responseUtils) {
                        getView().onSendMsgSuccess();
                    }

                    @Override
                    public void onError(String msg) {
                        getView().onSendMsgError(msg);
                    }
                });
    }

    /**
     * 减少信誉值
     * @param id
     */
    public void reduceCredit(String id) {
        RequestBody requestBody = new FormBody.Builder()
                .add("id", id)
                .build();
        RetrofitManager.getManager().getRetrofit()
                .create(API.class)
                .reduceCredit(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils responseUtils) {
                        if(responseUtils.getCode() == 200){
                            DataParam.getUserInfo().setCredit(DataParam.getUserInfo().getCredit()-0.1f);
                            getView().onCreditReduceSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().showMessage("更新信誉值失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
